package controllers;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import database.dao.Impl.BusinessDaoImpl;
import database.dao.Impl.CustomerDaoImpl;
import entity.Business;
import entity.Customer;
import entity.User;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import services.SystemContext;
import utils.MD5Util;

import javax.swing.text.html.ImageView;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @ClassName UserInfoViewController
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/19 16:33
 */
public class UserInfoViewController implements Initializable {

    public AnchorPane StorageChangePane;
    public TextField userNameField;
    public PasswordField passwordField;
    public TextField phoneField;
    public JFXComboBox sexComboBox;
    public JFXButton StorageBtn;
    public TextField addressField;

    private double xOffset;
    private double yOffset;

    public void closeAction(ActionEvent actionEvent) {
        Stage userInfoStage= ViewManager.STAGE.get("UserInfoStage");
        ViewManager.STAGE.remove("UserInfoStage");
        userInfoStage.close();
    }


    public void StorageChange(ActionEvent actionEvent) {
        String password=passwordField.getText();
        String userName=userNameField.getText();
        String user_sex ="null";

        if(sexComboBox.getSelectionModel()!=null)
            user_sex=((Label) sexComboBox.getSelectionModel().getSelectedItem()).getText();

        String address=addressField.getText();
        String Phone=phoneField.getText();
        String customer_id=SystemContext.getInstance().getId();

        if(!userName.equals("") && !password.equals("") && !Phone.equals("")&&!address.equals("")&&!user_sex.equals("null")) {

            if(SystemContext.getInstance().getIsCustomer()){
                Customer customer = new Customer(customer_id,userName, MD5Util.MD5(password), Phone, address, user_sex);
                CustomerDaoImpl customerDao = new CustomerDaoImpl();
                customerDao.updateCustomer(customer);
                SystemContext.getInstance().setUser(customer);
            }else{
                Business business = new Business(customer_id,userName, MD5Util.MD5(password), Phone, address, user_sex);
                BusinessDaoImpl businessDao = new BusinessDaoImpl();
                businessDao.updateBusiness(business);
                SystemContext.getInstance().setUser(business);
            }
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Stage stage= ViewManager.STAGE.get("UserInfoStage");

        sexComboBox.getItems().addAll(new Label("男"),new Label("女"));

        StorageChangePane.setOnMousePressed(event -> {
            xOffset = stage.getX() - event.getScreenX();
            yOffset = stage.getY() - event.getScreenY();
            StorageChangePane.setCursor(Cursor.CLOSED_HAND);
        });

        StorageChangePane.setOnMouseDragged(event -> {
            stage.setX(event.getScreenX() + xOffset);
            stage.setY(event.getScreenY() + yOffset);
        });
        StorageChangePane.setOnMouseReleased(event -> {
            StorageChangePane.setCursor(Cursor.DEFAULT);
        });

        SystemContext systemContext=SystemContext.getInstance();
        passwordField.setText(systemContext.getUser().getPassword());
        userNameField.setText(systemContext.getUser().getName());
        phoneField.setText(systemContext.getUser().getTelephone());
        addressField.setText(systemContext.getUser().getAddress());
        if(systemContext.getUser().getSex().equals("男"))
            sexComboBox.getSelectionModel().select(0);
        else
            sexComboBox.getSelectionModel().select(1);
    }
}
